﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 学生成绩管理系统
{
    public partial class frmadda : Form
    {
        public frmadda()
        {
            InitializeComponent();
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            string sno = this.TxtStuid.Text;
            string cno = this.TxtKcid.Text;
            string gradepeacetime = this.txtGradepeacetime.Text;
            string gradeexpriment = this.txtGradepeacetime.Text;
            string gradelast = this.txtGradeexpriment.Text;
            string grade = this.txtGrade.Text;
            if (sno == null || sno.Trim().Equals(""))
            {//学号不允许输入为空
                MessageBox.Show("请输入学号！", "提示");
                this.TxtStuid.Focus();
                return;
            }
            if (cno == null || cno.Trim().Equals(""))
            {//课程号不允许输入为空
                MessageBox.Show("请输入课程编号！", "提示");
                this.TxtKcid.Focus();
                return;
            }
            if (gradepeacetime != null && !gradepeacetime.Trim().Equals(""))
            {//平时不允许输入为空，并且必须为数字
                try
                {
                    decimal.Parse(gradepeacetime);
                }
                catch (Exception ex)
                {
                    ex.ToString();
                    MessageBox.Show("平时成绩请输入数字！", "提示");
                    this.txtGradepeacetime.Focus();
                    return;
                }
            }
            if (gradeexpriment != null && !gradeexpriment.Trim().Equals(""))
            {//实验成绩不允许输入为空，并且必须位数字
                try
                {
                    decimal.Parse(gradeexpriment);
                }
                catch (Exception ex)
                {
                    ex.ToString();
                    MessageBox.Show("实验成绩请输入数字！", "提示");
                    this.txtGradeexpriment.Focus();
                    return;
                }
            }
            if (gradelast != null && !gradelast.Trim().Equals(""))
            {//期末成绩不允许输入为空，并且必须为数字
                try
                {
                    decimal.Parse(gradelast);
                }
                catch (Exception ex)
                {
                    ex.ToString();
                    MessageBox.Show("期末成绩请输入数字！", "提示");
                    this.TxtGradelast.Focus();
                    return;
                }
            }
            StuGradeData data = new StuGradeData();
            data.Sno = sno;
            data.Cno = cno;
            data.Gradepeacetime = gradepeacetime;
            data.Gradeexpriment = gradeexpriment;
            data.Gradelast = gradelast;
            data.Grade = grade;
            try
            {
              
                    if (StuGradeOperation.inserStuGrade(data))
                    {//调用修改成绩函数
                        MessageBox.Show("录入成功！", "提示");
                        this.TxtKcid.Text = "";
                        this.TxtKcname.Text = "";
                        this.TxtStuid.Text = "";
                        this.TxtStuname.Text = "";
                        this.txtGrade.Text = "";
                        this.txtGradeexpriment.Text = "";
                        this.TxtGradelast.Text = "";
                        this.txtGradepeacetime.Text = "";
                        this.TxtStuid.Focus();
                    }
                    else
                    {
                        MessageBox.Show("录入失败！", "错误");
                    }
            }
            catch (Exception ex)
            {
                ex.ToString();
                MessageBox.Show("录入失败!", "错误");
            }
        }

        private void TxtStuid_TextChanged(object sender, EventArgs e)
        {
            if (this.TxtStuid.Text.Trim() != "")
            {
                StuentInfoData data = new StuentInfoData();
                data.Sno = this.TxtStuid.Text;
                DataSet deStu = StuentInfoOperation.getStuentInfo(data);
                if (deStu.Tables[0].Rows.Count > 0)
                {
                    this.TxtStuname.Text = deStu.Tables[0].Rows[0]["姓名"].ToString();
                }
            }
        }

        private void TxtKcid_TextChanged(object sender, EventArgs e)
        {
            if (this.TxtKcid.Text.Trim() != "")
            {
                CourseInfoData data = new CourseInfoData();
                data.Kcid = this.TxtKcid.Text;
                DataSet dsKc = CourseInfoOperation.getCourseInfo(data);
                if (dsKc.Tables[0].Rows.Count > 0)
                {
                    this.TxtKcname.Text = dsKc.Tables[0].Rows[0]["课程名称"].ToString();
                }
            }
        }
    }
}